package com.example.supermarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.supermarket.PageModel;
import com.example.supermarket.Result;
import com.example.supermarket.entity.Discounts;
import com.example.supermarket.entity.Employee;
import com.example.supermarket.entity.Goods;
import com.example.supermarket.service.DiscountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/discounts")
public class DiscountsController {
    @Autowired
    DiscountsService discountsService;
    @GetMapping("")
    public Result<PageModel<Discounts>> getGoodsList(@RequestParam (value = "query",required = false,defaultValue = "") String query,
                                                 @RequestParam("pagenum") Integer pagenum,
                                                 @RequestParam("pagesize") Integer pagesize){
        IPage<Discounts> ipage = new Page<>(pagenum,pagesize);
        QueryWrapper<Discounts> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("dis_name",query);
        IPage<Discounts> page  = discountsService.page(ipage,queryWrapper);
        PageModel<Discounts> goodsResult = new PageModel<>(page.getTotal(),page.getCurrent(),page.getRecords());
        return new Result<>(200,"ok",goodsResult);
    }
    @PostMapping("")
    public Result<Discounts> addDiscounts(@RequestBody Discounts discounts){
        boolean save = discountsService.save(discounts);
        QueryWrapper<Discounts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dis_name",discounts.getDisName())
                    .eq("dis_goods",discounts.getDisGoods())
                    .eq("dis_discount", discounts.getDisDiscount());
        Discounts one = discountsService.getOne(queryWrapper);
        if (save) {
            return new Result<>(200,"ok", one);
        }
        return new Result<>(0,"error", null);
    }
    /*删除员工*/
    @DeleteMapping("{id}")
    public Result<Discounts> deleteDiscounts(@PathVariable Integer id){
        boolean b = discountsService.removeById(id);
        if (b) {
            return new Result<Discounts>(200,"ok",null);
        }
        return new Result<Discounts>(0,"error",null);
    }
}

